#include <strings.h>
#include "cJSON.h"
#include "library.h"
#include "vqc.h"

JNIEXPORT jstring JNICALL Java_com_ruidcn_small_vqc_VQC_json
        (JNIEnv *env, jclass clazz, jstring in) {
    const char *in_buf = (*env)->GetStringUTFChars(env, in, JNI_FALSE);

    cJSON *json = cJSON_Parse(in_buf);

    bus_t *bus = bus_new();
    //根据传进的Json字符串获取对应结构体
    parse_json(json, bus);


    cJSON *json1 = cJSON_CreateObject();
    //打印结构体信息
    print_json(json1, bus);
    char *out_buf = cJSON_PrintUnformatted(json1);
    jstring out = (*env)->NewStringUTF(env, out_buf);
    cJSON_Delete(json);
    cJSON_Delete(json1);
    (*env)->ReleaseStringUTFChars(env, in, in_buf);
    bus_free(bus);
    return out;
}

JNIEXPORT jdoubleArray JNICALL Java_com_ruidcn_small_vqc_VQC_array
        (JNIEnv *env, jclass clazz, jdoubleArray in) {
    jdouble *in_dbl = (*env)->GetDoubleArrayElements(env, in, JNI_FALSE);
    (*env)->ReleaseDoubleArrayElements(env, in, in_dbl, JNI_ABORT);
//    (*env)->SetDoubleArrayRegion
//    (*env)->SetDoubleField

    jdouble buf[10];
    for (int i = 0; i < 10; ++i) {
        buf[i] = i * i;
    }

    jdoubleArray out = (*env)->NewDoubleArray(env, 10);
    (*env)->SetDoubleArrayRegion(env, out, 0, 10, buf);

    return out;
}
